clear
capture restore
set scheme cleanplots, perm

* SET DIRECTORIES
global home "..."

global data "$home/Data/"
global output "$home/Output/"

cd $home

*SET CONTROL VARIABLES
local firm_controls "log_salesg_real_1lag_std size_assets_1lag_std liquidity_1lag_std pcmq_1lag_std recpay2yq_1lag_std d2aq_1lag_std ln_market_cap_q_1lag_std tobins_q_1lag_std lev_d2c_1lag_std dd_1lag_std age_1lag_std i.sector i.fqtr"

local agg_controls "lngdp inflation unemp nasdaq fedfunds"

	*************
	***Options***
	*************
	*Set ivol measure to use as baseline
	local matur m1545

	* Set the LP details ========================================================================
	*# periods to compute the responses foreach
	local hor = 20
	*# lags of "endogenous" variables 
	local lagy = 4 
	*# lags of "shock"
	local lage = 0 

	********************************************************************************
	********************************************************************************
	********************************************************************************
	*Read-in ivol data (contains all trading days for Compustat firms)
	use "${data}Compustat_Quarterly_Final", clear
	
	merge m:1 year qtr using "${data}ramey_data"
	drop if _merge==2
	drop _merge
	
	local n = 20

	*Set panel data
	xtset firmid ts_qtr
	
	gen log_salesg_real_std = F.log_salesg_real_1lag_std

	*Flag sample for those in at least n quarters
	bysort gvkey pre_crisis: egen obs_num_temp = count(gvkey) if pre_crisis
	bysort gvkey: egen obs_num = max(obs_num_temp)

	*Flag outliers (bottom/top 0.5%) for investment
	_pctile investment_intensive if pre_crisis, p(.5 99.5)
	gen 	investment_outlier = (investment_intensive<r(r1)|investment_intensive>r(r2))

	gen sample_period=(pre_crisis==1)

	gen in_sample = (obs_num>=`n' & !missing(obs_num) & investment_outlier==0 & sample_period)

	********************************************************************************
	*Set panel data
	xtset firmid ts_qtr

	gen ivol_`matur'_std = ivol_`matur'
	gen vix_eoq_std = vix_eoq
	gen dd_std = dd
	gen age_1lag_std = age

	rename age age_1lag

	***Standardize quarterly Compustat variables and lagged ivol level
	foreach i in dd_1lag age_1lag log_salesg_real_1lag size_assets_1lag liquidity_1lag pcmq_1lag recpay2yq_1lag d2aq_1lag ln_market_cap_q_1lag tobins_q_1lag lev_d2c_1lag ivol_`matur' vix_eoq {

		*Pre-Crisis Standardization
		drop `i'_std
		qui: sum `i' if in_sample
		gen `i'_std = (`i' - r(mean)) / r(sd)
		
	}

	*Standardize ivol and dd
	_pctile ivol_`matur' if in_sample, percentiles(30 70)
	gen ivol_`matur'_hi2lo = (ivol_`matur' - r(r1)) / (r(r2) -  r(r1))

	_pctile dd  if in_sample, percentiles(30 70)
	gen dd_hi2lo = (dd - r(r1)) / (r(r2) -  r(r1))

	*Standardize mps to unit standard deviation & positive is expansionary
	egen date_tag=tag(ts_qtr)

	foreach s in aftr15 rrtaxu taxnews rameynews top3xsret mfev { 

		sum `s' if pre_crisis & date_tag
		replace `s' = -`s'/r(sd)
	
	}
	
	sum log_salesg_real_std if pre_crisis
	gen sales_gwth = log_salesg_real_std/r(sd)
	
	********************************************************************************
	*Prepare local projection variables
	********************************************************************************
	xtset firmid ts_qtr

	*Use the inflation-adjusted versions of dependent variables, if applicable
	foreach i in niq ivltq ivstq invtq intanq tanq xrdq saleq K_int K_int_Know K_int_Org K_int_offBS K_int_onBS {
		
		drop `i'
		rename `i'_real `i'
	 
	}


	foreach var in capital_stock K_int_Know_qtr K_int_Org_qtr {
		forvalues h =   0/`hor' {
			 gen `var'`h' = (ln(f`h'.`var') - ln(l.`var')) * 100
	}
	}

	*Create time variable for DK SEs
	xtset
	egen time=group(ts_qtr)
	xtset firmid time

	drop if !in_sample
		
foreach var in capital_stock  { 
	
	local i=2

		foreach s in rrtaxu sales_gwth aftr15 { 
			
		*Set policy shock to use as baseline
		local shock `s'
			

		eststo clear
			
		cap gen qtrs = _n-1 if _n<=`hor'+1
		cap gen zero =  0 	if _n<=`hor'+1
		cap gen b_shock = 0
		cap gen u_shock = 0
		cap gen d_shock = 0

		cap gen b_ivol = 0
		cap gen u_ivol = 0
		cap gen uu_ivol = 0 
		cap gen d_ivol = 0
		cap gen dd_ivol = 0

		cap gen b_shockI = 0
		cap gen u_shockI = 0
		cap gen uu_shockI = 0
		cap gen d_shockI = 0
		cap gen dd_shockI = 0

		cap gen b_intEff = 0

		cap gen nobs = 0
		
		********************************************************************************
		*INVESTMENT REGRESSIONS
		********************************************************************************

		*1. Investment on shock (plus interaction with ivol level)
		forvalues h = 0/`hor' {

			quietly: reghdfe `var'`h' `shock' L.ivol_`matur'_hi2lo  c.`shock'#c.L.ivol_`matur'_hi2lo `firm_controls' l(1/`lagy').(`agg_controls') if in_sample, a(firmid) vce(cluster time,dkraay(16)) old
			replace b_shock = _b[`shock']                     	if _n == `h'+1
			replace u_shock = _b[`shock'] + 1.645* _se[`shock']  	if _n == `h'+1
			replace d_shock = _b[`shock'] - 1.645* _se[`shock']  	if _n == `h'+1	
			
			replace b_ivol = _b[L.ivol_`matur'_hi2lo ]                     			if _n == `h'+1
			replace u_ivol = _b[L.ivol_`matur'_hi2lo ] + 1.645* _se[L.ivol_`matur'_hi2lo ]  	if _n == `h'+1
			replace d_ivol = _b[L.ivol_`matur'_hi2lo ] - 1.645* _se[L.ivol_`matur'_hi2lo ]  	if _n == `h'+1	
			replace uu_ivol = _b[L.ivol_`matur'_hi2lo ] + _se[L.ivol_`matur'_hi2lo ]  	if _n == `h'+1
			replace dd_ivol = _b[L.ivol_`matur'_hi2lo ] - _se[L.ivol_`matur'_hi2lo ]  	if _n == `h'+1	
			
			replace b_intEff = _b[`shock'] + _b[c.`shock'#c.L.ivol_`matur'_hi2lo ]                   	if _n == `h'+1
			
			replace b_shockI = _b[c.`shock'#c.L.ivol_`matur'_hi2lo ]                     			if _n == `h'+1
			replace u_shockI = _b[c.`shock'#c.L.ivol_`matur'_hi2lo ] + 1.645* _se[c.`shock'#c.L.ivol_`matur'_hi2lo ]  	if _n == `h'+1
			replace d_shockI = _b[c.`shock'#c.L.ivol_`matur'_hi2lo ] - 1.645* _se[c.`shock'#c.L.ivol_`matur'_hi2lo ]  	if _n == `h'+1	
			replace uu_shockI = _b[c.`shock'#c.L.ivol_`matur'_hi2lo ] + _se[c.`shock'#c.L.ivol_`matur'_hi2lo ]  	if _n == `h'+1
			replace dd_shockI = _b[c.`shock'#c.L.ivol_`matur'_hi2lo ] - _se[c.`shock'#c.L.ivol_`matur'_hi2lo ]  	if _n == `h'+1			
			replace nobs = e(N)                        						if _n == `h'+1	
			eststo
			
		}

		* Plot the Investment response
		twoway (rarea u_shockI d_shockI qtrs, fcolor(gs14) lcolor(gs14) lw(none) lpattern(solid) xlabel(,labsize(medlarge) nogrid) ylabel(,labsize(medlarge) nogrid)) ///
		(rarea uu_shockI dd_shockI qtrs, fcolor(gs8) lcolor(gs8) lw(none) lpattern(solid)) ///
		(line b_shockI qtrs, lcolor(black) lpattern(solid) lwidth(thick)) /// 
		(line zero qtrs, lcolor(black)) if !missing(qtrs), legend(off) ///
		ytitle("Percent", size(medlarge)) xtitle("Quarter", size(medlarge)) ///
		graphregion(color(white)) plotregion(color(white))

		graph export "${output}figureA6A7_`shock'.pdf", replace

		drop qtrs zero b_shock u_shock d_shock b_ivol u_ivol d_ivol b_shockI u_shockI uu_shockI d_shockI dd_shockI b_intEff uu_ivol dd_ivol nobs
		
		local i=`i'+20
		
		eststo clear	
	}

}
